﻿namespace Central.Linq {

	/// <summary>
	/// Класс описания агрегирующей функции
	/// </summary>
	public class AggregateFunctionDefinition {
		#region конструктор

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFunctionDefinition() {
			this.Function = AggregateFunction.None;
			this.IsMain = false;
		}

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFunctionDefinition(AggregateFunction function) {
			this.Function = function;
			this.IsMain = false;
		}

		/// <summary>
		/// Конструктор
		/// </summary>
		public AggregateFunctionDefinition(AggregateFunction function, bool isMain) {
			this.Function = function;
			this.IsMain = isMain;
		}

		#endregion конструктор

		/// <summary>
		/// Агрегирующая функция
		/// </summary>
		public AggregateFunction Function { get; set; }

		/// <summary>
		/// Признак основной функции
		/// </summary>
		public bool IsMain { get; set; }

		#region методы

		/// <summary>
		/// Сравнение объектов
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public override bool Equals(object obj) {
			return Equals(obj as AggregateFunctionDefinition);
		}

		/// <summary>
		/// Сравнение объектов AggregateFunctionDefinition
		/// </summary>
		/// <param name="other"></param>
		/// <returns></returns>
		public bool Equals(AggregateFunctionDefinition other) {
			if (other == null)
				return false;

			return other.Function.Equals(this.Function);
		}

		/// <summary>
		/// Получение Hash кода объекта
		/// </summary>
		/// <returns></returns>
		public override int GetHashCode() {
			//Calculate the hash code
			return this.Function.GetHashCode();
		}

		/// <summary>
		/// Перевод объекта в строку
		/// </summary>
		/// <returns>строка</returns>
		public override string ToString() {
			string ret = "";
			switch (this.Function) {
				case AggregateFunction.Average:
					ret = "SUM({0})/COUNT({0}<>null)";
					break;
				default:
					ret = this.Function.ToString() + "({0})";
					break;
			}
			return ret;
		}

		#endregion методы
	}
}